//
//  HYOUSingleton.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUSingleton.h"

@implementation HYOUSingleton

+ (instancetype)shareDataSingleton{
    static HYOUSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[HYOUSingleton alloc] init];
    }
    return dataBase;
}

static sqlite3 *db = nil;

-(void)openDB{
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"bestGift.sqlite"];
    NSLog(@"filePath = %@",filePath);
    BOOL ressult = sqlite3_open(filePath.UTF8String, &db);
    if (ressult == SQLITE_OK) {
        NSLog(@"成功打开数据库");
    }else{
        NSLog(@"打开失败");
    }
}

-(void)createHomeCellTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS HomeCellCollection(number INTEGER PRIMARY KEY AUTOINCREMENT, cover_image_url TEXT, title TEXT,content_url TEXT)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"homeCell创建成功");
    }else{
        NSLog(@"homeCell创建失败");
    }
}

-(void)insertHomeCellIntoTable:(HYOUOneCellModel *)list{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO HomeCellCollection(cover_image_url, title,content_url) VALUES('%@','%@','%@')",list.cover_image_url,list.title,list.content_url];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"homeCell插入成功");
    }else{
        NSLog(@"homeCell插入失败");
    }
}

-(NSMutableArray *)selectHomeCell:(HYOUOneCellModel *)list{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM HomeCellCollection WHERE title = '%@'", list.title];
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"homeCell查询准备成功");
    }else{
        NSLog(@"homeCell查询准备失败");
        return nil;
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *charcover_image_url = sqlite3_column_text(stmt, 1);
        const unsigned char *chartitle = sqlite3_column_text(stmt, 2);
        const unsigned char *charcontent_url = sqlite3_column_text(stmt, 3);
        
        NSString *cover_image_url = [NSString stringWithUTF8String:(const char *) charcover_image_url];
        NSString *title = [NSString stringWithUTF8String:(const char *) chartitle];
        NSString *content_url = [NSString stringWithUTF8String:(const char *)charcontent_url];
        
        HYOUOneCellModel *model = [[HYOUOneCellModel alloc] init];
        model.cover_image_url = cover_image_url;
        model.title = title;
        model.content_url = content_url;
        [array addObject:model];
    }
    NSLog(@"homeCell查询成功");
    return array;
}
-(NSMutableArray *)selectHomeCell{
    NSMutableArray *array = [NSMutableArray array];
    
    // 查询所有
    NSString *sql = @"SELECT * FROM HomeCellCollection";
    // 创建数据库跟随指针,主要是用于遍历查询表中每一行上的数据
    sqlite3_stmt *stmt = nil;   // 跟随指针
    // 准备查询工作
    // 参数3: -1代表sql语句不限长度
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"HomeCell准备成功");
    } else {
        NSLog(@"HomeCell查询准备失败");
        return nil;
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *charcover_image_url = sqlite3_column_text(stmt, 1);
        const unsigned char *chartitle = sqlite3_column_text(stmt, 2);
        const unsigned char *charcontent_url = sqlite3_column_text(stmt, 3);
        
        NSString *cover_image_url = [NSString stringWithUTF8String:(const char *) charcover_image_url];
        NSString *title = [NSString stringWithUTF8String:(const char *) chartitle];
        NSString *content_url = [NSString stringWithUTF8String:(const char *)charcontent_url];
        
        HYOUOneCellModel *model = [[HYOUOneCellModel alloc] init];
        model.cover_image_url = cover_image_url;
        model.title = title;
        model.content_url = content_url;
        [array addObject:model];
    }
    NSLog(@"homeCell查询成功");
    return array;
}
-(void)deleteHomeCellTable:(NSString *)name{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM HomeCellCollection WHERE title = '%@'",name];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"homecell删除成功");
    }else{
        NSLog(@"homecell删除失败");
    }
}

-(void)dropHomeCellTable{
    NSString *sql = @"DROP TABLE HomeCellCollection";
    BOOL reult = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (reult == SQLITE_OK) {
        NSLog(@"HomeCell删除表成功");
    }else{
        NSLog(@"HomeCell删除表失败");
    }
}
@end
